import time

import requests
import pytest
from api.reg_login_api import RegLoginApi
from common.assert_tools import common_assert
from common.clear_user_info import clear_user_info
from common.read_json_data import read_json


class TestRegLogin(object):

    @staticmethod
    def setup_class():
        # 清洗用户信息
        clear_user_info()

    def setup(self):
        self.ses = requests.session()
        self.reg_login = RegLoginApi(self.ses)

    def teardown(self):
        self.ses.close()

    # 获取图片验证码测试用例
    @pytest.mark.parametrize("r,exp_status_code", read_json("reg_login_data", "img_verify_code"))
    def test01_img_verify_code(self, r, exp_status_code):
        resp = self.reg_login.get_img_verify_code(r)
        assert exp_status_code == resp.status_code

    # 获取短信验证码测试用例
    @pytest.mark.parametrize("phone, imgVerifyCode, exp_status_code, exp_status, description",
                             read_json("reg_login_data", "phone_verify_code"))
    def test02_phone_verify_code(self, phone, imgVerifyCode, exp_status_code, exp_status, description):
        self.reg_login.get_img_verify_code(12356789)
        resp = self.reg_login.get_phone_verify_code(phone, imgVerifyCode)
        common_assert(resp, exp_status_code, exp_status, description)

    # 注册测试用例
    @pytest.mark.parametrize("form_dict, exp_status_code, exp_status, description",
                             read_json("reg_login_data", "user_register"))
    def test03_register(self, form_dict, exp_status_code, exp_status, description):
        self.reg_login.get_img_verify_code(12356789)
        self.reg_login.get_phone_verify_code(form_dict.get("phone"), 8888)
        resp = self.reg_login.user_register(form_dict)
        common_assert(resp, exp_status_code, exp_status, description)

    # 登录测试用例
    @pytest.mark.parametrize("mobile, password, exp_status_code, exp_status, description",
                             read_json("reg_login_data", "user_login"))
    def test04_user_login(self, mobile, password, exp_status_code, exp_status, description):
        resp = self.reg_login.user_login(mobile, password)
        common_assert(resp, exp_status_code, exp_status, description)

    @pytest.mark.skip
    def test05_login_pwd_err(self):
        for i in range(1, 4):
            resp = self.reg_login.user_login("13100002233", "qq123")
            common_assert(resp, 200, 100, "错误")
            print("密码错误第{}次".format(i))
        time.sleep(60)
        resp = self.reg_login.user_login("13100002233", "qq1234")
        common_assert(resp, 200, 200, "成功")
